#!/bin/python

# --------------------------------------
# VIDEO BUDDHA 2 - edit definitions window
# --------------------------------------
# Ranames files based on a specific regex pattern in 
#  show_list.db and based on a specific list of file 
#  extensions.
# --------------------------------------

import wx

import renamer.db.database
import views.win_edit_selected_def

class WinEditDefs(wx.Frame):
	
	def __init__(self):
		wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, u"Edit Show Definitions", wx.DefaultPosition, wx.Size(570, 300), wx.DEFAULT_FRAME_STYLE ^ (wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX))
		
		# Db connect that shit
		self.db = renamer.db.database.Database()
		
		# Application icon
		favicon = wx.Icon('res/favicon.ico', wx.BITMAP_TYPE_ICO, 16, 16)
		self.SetIcon(favicon)
		
		self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
		
		sizer_top = wx.BoxSizer(wx.VERTICAL)
		
		self.panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
		sizer = wx.BoxSizer(wx.VERTICAL)
		
		# The list control
		self.list_ctrl = wx.ListCtrl(self.panel, size = (-1, -1), style=wx.LC_REPORT | wx.BORDER_SUNKEN)
		self.list_ctrl.InsertColumn(0, '#', wx.LIST_AUTOSIZE_USEHEADER, 20)
		self.list_ctrl.InsertColumn(1, 'Old Name', wx.LIST_AUTOSIZE_USEHEADER, 250)
		self.list_ctrl.InsertColumn(2, 'New Name', wx.LIST_AUTOSIZE_USEHEADER, 250)
		sizer.Add( self.list_ctrl, 1, wx.EXPAND|wx.ALL, 5 )
		
		# Fill the list control
		self.update_list_ctrl()
		
		sizer_buttons = wx.BoxSizer(wx.HORIZONTAL)
		
		
		sizer_buttons.AddSpacer((5, 0), 0, wx.EXPAND, 0)
		
		self.m_staticText2 = wx.StaticText(self.panel, wx.ID_ANY, u"Old Name", wx.DefaultPosition, wx.DefaultSize, 0)
		self.m_staticText2.Wrap(-1)
		sizer_buttons.Add(self.m_staticText2, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
		
		self.text_ctrl_odlname = wx.TextCtrl(self.panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_buttons.Add(self.text_ctrl_odlname, 1, wx.ALL, 5)
		
		
		sizer_buttons.AddSpacer((5, 0), 0, wx.EXPAND, 5)
		
		self.m_staticText3 = wx.StaticText(self.panel, wx.ID_ANY, u"New Name", wx.DefaultPosition, wx.DefaultSize, 0)
		self.m_staticText3.Wrap(-1)
		sizer_buttons.Add(self.m_staticText3, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
		
		self.text_ctrl_newname = wx.TextCtrl(self.panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_buttons.Add(self.text_ctrl_newname, 1, wx.ALL, 5 )
		
		sizer.Add(sizer_buttons, 0, wx.EXPAND, 0)
		
		sizer_buttons2 = wx.BoxSizer(wx.HORIZONTAL)
		
		# Add, Edit, Delete buttons
		self.button_add = wx.Button(self.panel, wx.ID_ANY, u"Add", size = (125, -1))
		sizer_buttons2.Add(self.button_add, 0, wx.ALL, 5)
		
		
		sizer_buttons2.AddSpacer((0, 0), 1, wx.EXPAND, 5)
		
		self.button_edit = wx.Button(self.panel, wx.ID_ANY, u"Edit", wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_buttons2.Add(self.button_edit, 0, wx.EXPAND|wx.ALL, 5)
		
		self.button_delete = wx.Button(self.panel, wx.ID_ANY, u"Delete", wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_buttons2.Add(self.button_delete, 0, wx.ALL, 5)
		
		sizer.Add(sizer_buttons2, 0, wx.EXPAND, 5)
		
		self.panel.SetSizer(sizer)
		self.panel.Layout()
		sizer.Fit(self.panel)
		sizer_top.Add(self.panel, 1, wx.EXPAND, 0)
		
		self.SetSizer(sizer_top)
		self.Layout()
		
		self.Centre(wx.BOTH)
		
		self.button_add.Bind(wx.EVT_BUTTON, self.on_add)
		self.button_edit.Bind(wx.EVT_BUTTON, self.on_edit)
		self.button_delete.Bind(wx.EVT_BUTTON, self.on_delete)
		self.list_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.on_edit)
	
	def __del__(self):
		self.Close()
		
	def update_list_ctrl(self):
		self.list_ctrl.DeleteAllItems()
		
		rows = self.db.retrieve()		
		
		index = 0
		for row in rows:
			self.list_ctrl.InsertStringItem(index, str(row[0]))
			self.list_ctrl.SetStringItem(index, 1, row[1])
			self.list_ctrl.SetStringItem(index, 2, row[2])
			
			# Alt row colors
			if index % 2:
				self.list_ctrl.SetItemBackgroundColour(index, wx.Colour(224, 224, 255))
			
			index += 1
			
	def on_add(self, event):
		oldname = self.text_ctrl_odlname.GetValue()
		newname = self.text_ctrl_newname.GetValue()
		
		if oldname == '' or newname == '':
			dialog = wx.MessageDialog(self, "Both text fields are empty.", "Error!", wx.OK)
			dialog.ShowModal()
			dialog.Destroy()
			
			return False
		else:
			# Insert and update the listings		
			self.db.insert(oldname, newname)
			self.update_list_ctrl()
			
			# Empty the text controls
			self.text_ctrl_odlname.Clear()
			self.text_ctrl_newname.Clear()
			
	def on_edit(self, event):
		index = self.list_ctrl.GetFirstSelected()
		
		try:
			col = self.list_ctrl.GetItem(index, 0).GetText()

			window_edit_selected_def = views.win_edit_selected_def.WinEditSelectedDef(col)
			window_edit_selected_def.Show()
		except Exception:
			dialog = wx.MessageDialog(self, "The column number was not found.", "Error!", wx.OK)
			dialog.ShowModal()
			dialog.Destroy()
		
		self.update_list_ctrl()
			
	def on_delete(self, event):
		index = self.list_ctrl.GetFirstSelected()
		col = self.list_ctrl.GetItem(index, 0).GetText()

		self.db.delete(col[0])
		
		self.update_list_ctrl()